package com.offcn.member.service.impl;


import com.offcn.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.common.utils.PageUtils;
import com.offcn.member.dao.MemberDao;
import com.offcn.member.entity.MemberEntity;
import com.offcn.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("memberService")

public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<MemberEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",params.get("key")).or().like("nickname",params.get("key"));
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                queryWrapper

        );

        return new PageUtils(page);
    }

    /**
     * 用户注册统计
     */
    @Autowired
    private MemberDao memberDao;
    @Override
    public List<Map<String, Object>> countByDateTime(String beginTime, String endTime) {
    QueryWrapper<MemberEntity> queryWrapper=new QueryWrapper<>();
    queryWrapper.select("DATE_FORMAT(create_time,'%y-%m-%d') as time,count(*) as num")
            .groupBy("time")
            .between("create_time",beginTime+" 00:00:00",endTime+" 23:59:59");
        List<Map<String, Object>> list = memberDao.selectMaps(queryWrapper);
        return list;
    }

    /**
     * 登入校验
     * @param username
     * @param password
     * @return
     */
    @Override
    public MemberEntity login(String username, String password) {

        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username).eq("PASSWORD",password);
        MemberEntity memberEntity = this.getOne(queryWrapper);
        return memberEntity;
    }

}